﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Skyworth.CRM.Common;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.SalesManagement
{
    public partial class AdjustSpecial:BasePage
    {
        protected int totalRecords = 0;
        protected int totalPages = 1;

        private static IPolicySchemeBLL policySchemeBLL = IocContainer.Instance.Resolve<IPolicySchemeBLL>();
        private static IPricePolicyBLL policyBLL = IocContainer.Instance.Resolve<IPricePolicyBLL>();

        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if (!Page.IsPostBack)
            {
                // 按钮权限控制
                ControlAuthority(new List<WebControl> { btnUpdate,btnSearch }, 14);
            }
            Query();
        }



        protected void Query()
        {
            int pageIndex = 0;
            int.TryParse(this.pageNum.Value, out pageIndex);
            int pagePerSize = 0;
            int.TryParse(this.numPerPageSelect.Value, out pagePerSize);

            StringBuilder strWhere = new StringBuilder(" where 1=1 and SpecialType=0 ");
            if (!string.IsNullOrEmpty(this.txtName.Text))
            {
                strWhere.Append(" and SchemeName like '%" + this.txtName.Text + "%'");
            }
            if (!string.IsNullOrEmpty(this.txtCode.Text))
            {
                strWhere.Append(" and SchemeCode like '%" + this.txtCode.Text + "%'");
            }

            if (!string.IsNullOrEmpty(this.txtQueryName.Text))
            {
                strWhere.Append(" and p.PolicyName like '%" + this.txtQueryName.Text + "%'");
            }


            StringBuilder strOrderBy = new StringBuilder(" Order by  ");
            strOrderBy.Append(!string.IsNullOrEmpty(this.orderField.Value) ? " " + this.orderField.Value : "  SchemeName  ");
            strOrderBy.Append(!string.IsNullOrEmpty(this.orderValue.Value) ? " " + this.orderValue.Value : "  asc ");

             totalRecords = 0;
             totalPages = 0;
            DataTable dtProducts = policySchemeBLL.GetPoliciesByPage(strWhere.ToString(), strOrderBy.ToString(), pageIndex, pagePerSize, out totalRecords,
                                           out totalPages);

            this.EmpTableBody.Text = BuildEmployeeTable(dtProducts);
        }

        protected string BuildEmployeeTable(DataTable dtEmployee)
        {
            StringBuilder result = new StringBuilder();
            if (dtEmployee != null && dtEmployee.Rows.Count > 0)
            {
                int rowNum = 1;
                foreach (DataRow row in dtEmployee.Rows)
                {
                    result.Append(rowNum % 2 == 0 ? "<tr class='trbg' rel='" + row["Id"] + "' rel2='" + row["ProductId"] + "'>" : "<tr rel='" + row["Id"] + "' rel2='" + row["ProductId"] + "'>");
                    result.AppendFormat("<td>{0}</td>", row["SchemeCode"]);
                    result.AppendFormat("<td>{0}</td>", row["SchemeName"]);
                    result.AppendFormat("<td>{0}</td>", row["PriceType"]);
                    result.AppendFormat("<td>{0}</td>", row["Price"]);
                    result.AppendFormat("<td>{0}</td>", row["TaxRatio"]);
                    result.AppendFormat("<td>{0}</td>", row["TaxPrice"]);
                   
                    result.AppendFormat("<td>{0}</td>", row["ProductName"]);
                    result.AppendFormat("<td>{0}</td>", row["StartDate"]);
                    result.AppendFormat("<td>{0}</td>", row["EndDate"]);
                    result.AppendFormat("<td>{0}</td>", row["SpecialType"]);
                    result.Append("</tr>");
                    rowNum++;
                }
            }
            return result.ToString();

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.pageNum.Value = "1";
            Query();
        }

        protected void btnhidDelete_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    int empId = 0;
            //    int.TryParse(this.selectedEmpID.Value, out empId);
            //    employeeBLL.DeleteEmployee(empId);

            //    Query();
            //}
            //catch (Exception exception)
            //{
            //    Logger.Instance.Error(exception);
            //    Alert("发生异常：\r\n" + exception.Message);
            //}

        }
    }
}
